package com.hk.blog.controller;

import com.hk.blog.domain.Menu;
import com.hk.blog.domain.Permission;
import com.hk.blog.service.MenuService;
import com.hk.blog.service.PermissionMenuService;
import com.hk.blog.service.PermissionService;
import com.hk.blog.vo.PermissionMenuBean;
import com.hk.commons.JsonResult;
import com.hk.commons.query.Order;
import com.hk.commons.query.QueryModel;
import com.hk.commons.util.Constants;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * @author Kevin
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("perms")
public class PermissionController {

    private final PermissionService permissionService;

    private final PermissionMenuService permissionMenuService;

    private final MenuService menuService;

    @PostMapping("list")
    public JsonResult<?> list(@RequestBody QueryModel<Permission> query) {
        if (query.getOrders().isEmpty()) {
            query.addOrders(Order.desc("createdDate"));
        }
        return JsonResult.success(permissionService.queryForPage(query));
    }

    @GetMapping("get")
    public JsonResult<?> get(Long id) {
        List<Menu> menuList = menuService.findByParentId(Constants.DEFAULT_VALUE_LONG);
        var treeMenuList = toMenuSelectTree(menuList);
        Map<String, Object> result = new HashMap<>(2);
        result.put("menuList", treeMenuList);
        result.put("menuIds", permissionMenuService.findMenuIdsByPermissionId(id));
        result.put("permission", Objects.isNull(id) ? null : permissionService.findById(id).orElse(null));
        return JsonResult.success(result);
    }

    private List<Map<String, ?>> toMenuSelectTree(List<Menu> menuList) {
        return menuList.stream()
                .map(item -> {
                    List<Menu> childrenMenuList = menuService.findByParentId(item.getId());
                    boolean isLeaf = childrenMenuList.isEmpty();
                    List<Map<String, ?>> children = null;
                    if (!isLeaf) {
                        children = toMenuSelectTree(childrenMenuList);
                    }
                    var m = new HashMap<String, Object>();
                    m.put("value", item.getId());
                    m.put("isLeaf", isLeaf);
                    m.put("disabled", !isLeaf);
                    m.put("title", item.getMenuName());
                    if (!isLeaf) {
                        m.put("children", children);
                    }
                    return m;
                })
                .collect(Collectors.toList());
    }

    @PostMapping("delete")
    public JsonResult<?> deleteById(Long id) {
        permissionService.deleteById(id);
        return JsonResult.success();
    }

    @PostMapping("save")
    public JsonResult<?> saveOrUpdate(@RequestBody PermissionMenuBean permission) {
        permissionService.insertOrUpdate(permission);
        return JsonResult.success();
    }
}
